package kr.hkit.powerjava.chapter26.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

import kr.hkit.powerjava.chapter26.dao.EmployeeDAO;
import kr.hkit.powerjava.chapter26.dto.Employee;

public class EmployeeService extends AbsService<Employee> {
	private ArrayList<Employee> list;
	public EmployeeService(Scanner sc, Connection connection) {
		super(sc, connection);
		dao = new EmployeeDAO(connection);
	}

	@Override
	public Employee inputItemInfo(boolean gubun) {
		System.out.println("부서번호 이름");
		Employee employee = new Employee(sc.nextInt(), sc.next().trim());
		
		if(gubun) {
			System.out.println("직급 선임자 봉급 학과번호");
			employee.setTitle(sc.next().trim());
			employee.setManager(sc.nextInt());
			employee.setSalary(sc.nextInt());
			employee.setDno(sc.nextInt());
		}
		return employee;
	}

	@Override
	public void prnSubMenu() {
		System.out.println("1.이름순 정렬\n2.봉급별 정렬\n3.번호순 정렬");
		try {
			int res = sc.nextInt();
			list = dao.listItems();
			switch(res) {
				case 1:
					prnListItem(new Comparator<Employee>() {
						@Override
						public int compare(Employee o1, Employee o2) {
							return o1.getEmpName().hashCode() - o2.getEmpName().hashCode();
						}
					});
					break;
				case 2:
					prnListItem(new Comparator<Employee>() {
						@Override
						public int compare(Employee o1, Employee o2) {
							return o1.getTitle().hashCode() - o2.getTitle().hashCode();
						}
					});
					break;
				default:
					prnListItem(null);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void prnListItem(Comparator<Employee> comp) {
		if(comp == null) {
			Collections.sort(list);
			prnList(list);
		} else {
			Collections.sort(list, comp);
			prnList(list);
		}
	}

	private void prnList(ArrayList<Employee> list) {
		for(Employee emp : list) {
			System.out.println(emp);
		}
	}	
}